package com.paw.oauth2.server.config;

import com.paw.oauth2.server.service.DefaultUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @author lvzihai
 * @date 2021/7/14
 **/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Autowired
  private DefaultUserDetailService userDetailService;


  @Bean
  @ConditionalOnMissingBean
  public PasswordEncoder passwordEncoder(){
    // 不加密
//    return NoOpPasswordEncoder.getInstance();
    return new BCryptPasswordEncoder();
  }

  // 内存 userDetailService
//  @Bean
//  @Override
//  protected UserDetailsService userDetailsService() {
//    InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
//    String password = passwordEncoder().encode("123456");
//    System.out.println("password: "+password);
//    manager.createUser(User.withUsername("user_1").password(password).authorities("USER").build());
//
//    return manager;
//  }

  @Override
  @Bean
  public AuthenticationManager authenticationManagerBean() throws Exception {

    return super.authenticationManagerBean();
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
//    http.csrf().disable();
//    http.requestMatchers().anyRequest()
//        .and()
//        .authorizeRequests()
//        .antMatchers("/oauth/**").permitAll();
//    //支持表单登录
//    http.formLogin();

    http.formLogin()
        // 指定login地址 controller需要实现
        .loginPage("/login")
        .and()
        .authorizeRequests()
        .antMatchers("/login","/oauth/**").permitAll()
        .anyRequest()
        .authenticated()
        .and().csrf().disable().cors();
  }


  @Override
  public void configure (WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/assets/**", "/css/**", "/images/**");
  }

  @Override
  protected void configure (AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailService);
  }
}
